//#ifndef decode_js
//#define decode_js 1

var decode = {

  // decode a base 85 string to a number
  toNumber : function (s) {
    s=s.replace(/~/g,'\\');
    for (var i=s.length, r=0; i--;) {
      r += Math.pow(85, i) * (s.charCodeAt(i) - 35);
    } return r;
  },
  
  // decode a base 85 string to a byte array
  toArray : function (s) {
    var i, r=[], l=s.length, c, q='';
    for (i=0; i<l; i++) {
      q += (c = s.charAt(i));
      if (c != ' ' && q.length < 5) continue;
      this.decodeChunk(q, r);
      q = '';
    }
    if (q) this.decodeChunk(q, r);
    return r;
  },
  
  
  // decode a base 85 string to a normal string
  //#ifdef DECODE_STRING
  toString : function (s) {
    var i, a = this.toArray(s), l=a.length, r='';
    for (i=0; i<l; i++) r += String.fromCharCode(a[i]);
    return r;
  },
  //#endif
  
  // decode a base 85 chunk
  decodeChunk : function (s, a) {
    for (var n=this.toNumber(s.trim()), p=4; p--;) {
      a.push(n%256);
      if (!n) break;
      n = Math.floor(n/256);
    }
  }
  
};

//#endif

